import * as Notifications from "expo-notifications";

export const useRegisterForPushNotificationsAsync = async () => {
  const { status: existingStatus } = await Notifications.getPermissionsAsync();
  let finalStatus = existingStatus;
  if (existingStatus !== "granted") {
    const { status } = await Notifications.requestPermissionsAsync();
    finalStatus = status;
  }
  if (finalStatus !== "granted") {
    alert("Failed to get push token for push notification!");
    return;
  }
  console.log("Permission granted!");
};
export const scheduleLocalNotification = async () => {
  await Notifications.scheduleNotificationAsync({
    content: {
      title: "你有一条新消息",
      body: "这是本地通知的内容",
      data: { key: "value", screen: "system" },
    },
    trigger: {
      type: Notifications.SchedulableTriggerInputTypes.TIME_INTERVAL,
      seconds: 5,
      repeats: false,
    },
  });
};

export const setupNotificationListeners = () => {
  Notifications.addNotificationResponseReceivedListener(response => {
    const { data } = response.notification.request.content;
    if (data && data.screen === "system") {
      console.log("Navigating to system");
    } else {
      console.log("Unknown screen:", data.screen);
    }
  });
};
